package com.yiwei.ydd.activity;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yiwei.ydd.R;
import com.yiwei.ydd.adapter.MyMessageTotalAdapter;
import com.yiwei.ydd.api.Api;
import com.yiwei.ydd.api.bean.MessageOneTypeBean;
import com.yiwei.ydd.base.BaseActivity;
import com.yiwei.ydd.constant.Const;
import com.yiwei.ydd.util.Network;
import com.yiwei.ydd.util.RxLifeUtil;
import com.yiwei.ydd.util.ToastUtil;
import com.yiwei.ydd.view.V19FrameLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 17-12-8.
 */

public class MyMessageTotalActivity extends BaseActivity {
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_right)
    TextView txtRight;
    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.list)
    RecyclerView list;
    @BindView(R.id.refresh)
    SwipeRefreshLayout refresh;


    private Unbinder unbiner;
    private MyMessageTotalAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_message_total);
        initBar();
        unbiner = ButterKnife.bind(this);
        init();
    }

    private void init() {
        txtTitle.setText("我的消息");
        refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getMesgIndex();
            }
        });
        adapter = new MyMessageTotalAdapter(this);
        list.setAdapter(adapter);
    }

    @Override
    protected void onResume() {
        super.onResume();
        getMesgIndex();
    }

    private void getMesgIndex() {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }

        refresh.setRefreshing(true);

        Api.api_service.getMesgIndex().compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        refresh.setRefreshing(false);
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {

                    if (result.datas != null) {
//                        if (result.datas.system.count == 0 && result.datas.money.count == 0 && result.datas.orders.count == 0 && result.datas.comm.count == 0) {
//                            layoutEmpty.setVisibility(View.VISIBLE);
//                            return;
//                        }
//                        layoutEmpty.setVisibility(View.GONE);
                        List<MessageOneTypeBean> list = new ArrayList<MessageOneTypeBean>();
                        if (result.datas.system != null) {
                            MessageOneTypeBean system = new MessageOneTypeBean(result.datas.system.count, result.datas.system.item, Const.MsgType.SYSTEM);
                            list.add(system);
                        }
                        if (result.datas.money != null) {
                            MessageOneTypeBean system = new MessageOneTypeBean(result.datas.money.count, result.datas.money.item, Const.MsgType.MONEY);
                            list.add(system);
                        }
                        if (result.datas.orders != null) {
                            MessageOneTypeBean system = new MessageOneTypeBean(result.datas.orders.count, result.datas.orders.item, Const.MsgType.ORDERS);
                            list.add(system);
                        }
                        if (result.datas.comm != null) {
                            MessageOneTypeBean system = new MessageOneTypeBean(result.datas.comm.count, result.datas.comm.item, Const.MsgType.COMM);
                            list.add(system);
                        }
                        adapter.update(list);
                    } else {

                    }
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbiner.unbind();
        unbiner = null;
    }

    @OnClick(R.id.btn_back)
    public void onViewClicked() {
        finish();
    }
}
